package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.nfc.Tag;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.DynamicBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class DynamicAdapter extends RecyclerView.Adapter<DynamicAdapter.MyHolder> {

    private Context mContext;
    public List<DynamicBean.DataBean> list = null;
    private OnLister lister;

    private DynamicImgAdapter imgAdapter;

    public DynamicAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_dynamic, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position1) {
        final DynamicBean.DataBean dynamicBean = list.get(position1);

        Glide.with(mContext).load(dynamicBean.getTeacherImg())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext,100))
                .placeholder(R.mipmap.icon_user)
                .error(R.mipmap.icon_user)
                .override(150,150)
                .into(viewHolder.imgDynamicHeader);

        viewHolder.tvDynamicName.setText(dynamicBean.getTeacherName());
        viewHolder.tvDynamicTime.setText(dynamicBean.getCreateTime());
        viewHolder.tvContent.setText(dynamicBean.getContent());
        viewHolder.tvThumb.setText(dynamicBean.getLikeNum() + "人点赞");
        imgAdapter = new DynamicImgAdapter(mContext, new DynamicImgAdapter.OnImgLister() {
            @Override
            public void imgLister(int position) {
                if (lister != null) {
                    lister.listerImage(position1, position);
                }
            }
        });

        /** 控制图片显示列数 **/
        int item = 1;
        if (dynamicBean.getUrlArray() != null) {
            if (dynamicBean.getUrlArray().size() > 0) {
                if (dynamicBean.getUrlArray().size() == 1) {
                    item = 1;
                } else {
                    item = 3;
                }
            }
        }

        /** 图片列表 **/
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, item) {
            @Override
            public boolean canScrollVertically() {
                return false;//禁止滑动
            }
        };
        viewHolder.recyclerviewImageview.setLayoutManager(gridLayoutManager);
        viewHolder.recyclerviewImageview.setAdapter(imgAdapter);
        imgAdapter.setData(dynamicBean.getUrlArray());
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<DynamicBean.DataBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<DynamicBean.DataBean> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgDynamicHeader;          //发布人头像
        public TextView tvDynamicName;              //发布人名字
        public TextView tvDynamicTime;              //发布时间
        public TextView tvContent;                  //发布内容
        public ImageView imgLike;                   //点赞按钮
        public ImageView imgShare;                  //分享按钮
        public TextView tvThumb;                    //点赞过的人
        public LinearLayout lyThumb;                //点赞过的这一行
        public RecyclerView recyclerviewImageview;  //发布图片条目

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgDynamicHeader = itemView.findViewById(R.id.img_dynamic_header);
            tvDynamicName = itemView.findViewById(R.id.tv_dynamic_name);
            tvDynamicTime = itemView.findViewById(R.id.tv_dynamic_time);
            tvContent = itemView.findViewById(R.id.tv_content);
            imgLike = itemView.findViewById(R.id.img_like);
            imgShare = itemView.findViewById(R.id.img_share);
            tvThumb = itemView.findViewById(R.id.tv_thumb);
            lyThumb = itemView.findViewById(R.id.ly_thumb);
            recyclerviewImageview = itemView.findViewById(R.id.recyclerview_imageview);
        }
    }

    public interface OnLister {

        /**
         * 选中照片
         *
         * @param position1
         * @param position2
         */
        void listerImage(int position1, int position2);
    }
}